//
//  ViewController.m
//  QRMaster
//
//  Created by myApple on 15/11/9.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

enum CGLabelTag
{
    KCTABBAR_QRM_MIANPAGE = 100000,
    KCTABBAR_QRM_GROUP,
    KCTABBAR_QRM_QUICK,
    KCTABBAR_QRM_FOUND,
    KCTABBAR_QRM_MINE,
};

#import "QRTabbarController.h"
#import "CommunityManageViewController.h"
#import "MianViewController.h"
#import "BackPageViewController.h"
#import "DimensionalCardViewController.h"
#import "FindViewController.h"
#import "PersonalCenterViewController.h"
#import "UserCenterViewController.h"
#import "QRNavigationController.h"
#import "QRTabbarView.h"
#import "ManagementController.h"

#import "CommonCardViewController.h"

@interface QRTabbarController ()<QRTabbarViewDelegate>
{
    UIImageView * tabImgView;
    UIImageView * tabbarMainView;
    UIView * lineView;//tabbar上面的分隔线
    QRNavigationController * QRNV;
    
    NSArray * imageArray;
    NSArray * nameArray;
    
     QRTabbarView * myTabbar;
}

@end

@implementation QRTabbarController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moveTabbarOrNot:) name:@"moveTabbarOrNot" object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    UIViewController * uvc = nil;
    NSMutableArray * array = [[NSMutableArray alloc]init];
    
    //    uvc = [[MianViewController alloc]init];
    //    QRNV = [[QRNavigationController alloc]initWithRootViewController:uvc];
    //    [array addObject:QRNV];
    
    uvc = [[BackPageViewController alloc]init];
    QRNV = [[QRNavigationController alloc]initWithRootViewController:uvc];
    [array addObject:QRNV];
    
//    uvc = [[CommunityManageViewController alloc]init];
//    QRNV = [[QRNavigationController alloc]initWithRootViewController:uvc];
//    [array addObject:QRNV];
//    
    uvc = [[ManagementController alloc]init];
    QRNV = [[QRNavigationController alloc]initWithRootViewController:uvc];
    [array addObject:QRNV];
    
//    uvc = [[DimensionalCardViewController alloc]init];
//    QRNV = [[QRNavigationController alloc]initWithRootViewController:uvc];
//    [array addObject:QRNV];
    
    uvc = [[FindViewController alloc]init];
    QRNV = [[QRNavigationController alloc]initWithRootViewController:uvc];
    [array addObject:QRNV];
    
//    uvc = [[PersonalCenterViewController alloc]init];
//    QRNV = [[QRNavigationController alloc]initWithRootViewController:uvc];
//    [array addObject:QRNV];
    
    uvc = [[UserCenterViewController alloc]init];
    QRNV = [[QRNavigationController alloc]initWithRootViewController:uvc];
    [array addObject:QRNV];
    
    self.viewControllers = array;
    
     [self prapreTabbar];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [QRSingleton sharedQRSingleton].currentAreaCodeString = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentAreaCode"];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

-(void)prapreTabbar
{
    CGRect frame = [[UIScreen mainScreen] bounds];
    myTabbar = [[QRTabbarView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 49)];
    myTabbar.delegate = self;
    [myTabbar prepareBtn];
    [self.tabBar addSubview:myTabbar];
    
    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, 49);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setBackgroundImage:img];
    [self.tabBar setShadowImage:img];
    myTabbar.backgroundColor = [UIColor whiteColor];
    
}

-(void)pressedCustomTabbar:(NSInteger)index
{
    self.selectedIndex = index;
}

-(void)moveTabbarOrNot:(NSNotification *)not
{
    NSString * flag = not.object;
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([flag isEqualToString:@"1"]) {
            tabImgView.hidden = YES;
            tabbarMainView.hidden = YES;
            lineView.hidden = YES;
        }else
        {
            
            [UIView animateWithDuration:0.5
                             animations:^{
                                 tabImgView.hidden = NO;
                                 tabbarMainView.hidden = NO;
                                 lineView.hidden = NO;
                             }
                             completion:^(BOOL finished) {
                                 
                             }];
        }
        
    });
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)setTabBarHidden
{
    tabImgView.hidden=YES;
}
@end
